/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */

package org.ceroproject.ui.commandline.commands;

import java.util.Collection;

import org.ceroproject.games.Game;
import org.ceroproject.games.Zone;
import org.ceroproject.ui.commandline.Command;
import org.ceroproject.ui.commandline.CommandLineLauncher;


/**
 * @author Roux Camille
 */
public class Lister extends Command {

	private static Lister instance = new Lister();

	private Lister() {
		super("list");
	}

	public static Lister getCommand() {
		return instance;
	}

	@Override
	public String toString() {
		return "list elements of the game";
	}

	public boolean executer(String parametres) {
		Game game = CommandLineLauncher.getGame();
		if (game == null || parametres.trim() == "")
			return false;
		String[] split = parametres.split(":");

		Collection collection = null;
		if (split[0].equals("p"))
			collection = game.getPlayers().getSortedList();
		else if (split[0].equals("a"))
			collection = game.getActions();
		else if (split[0].equals("r"))
			collection = game.getRules();
		else if (split[0].equals("z")) {
			if (split.length == 1)
				collection = game.getZones();
			else {
				collection = game.getPlayers().get(Integer.valueOf(split[1]))
						.getZones();
			}
		} else if (split[0].equals("c")) {
			if (split.length == 2) {
				Zone zone = game.getZones().get(split[1]);
				if (zone != null)
					collection = zone.getSortedList();
				else
					System.out.println("no such zone");
			}
			if (split.length == 3) {
				collection = game.getPlayers().get(Integer.valueOf(split[1]))
						.getZones().get(split[2]).getSortedList();
			}
		} else if (split[0].equals("rounds"))
			collection = game.getRounds();
		else if (split[0].equals("ar"))
			collection = game.getActionRules().getSortedList();
		else {
			System.out.println("bad argument(s)");
		}

		if (collection != null)
			for (Object obj : collection)
				System.out.println(obj.toString());

		return true;
	}

	@Override
	public String help() {
		return "lists a part of the game\n" + "syntax:\n"
				+ "list:p  :  lists the players\n"
				+ "list:a  :  lists the available actions\n"
				+ "list:r  :  lists the rules\n"
				+ "list:z  :  lists the zones\n"
				+ "list:z:playerid  :  lists the zones of a player\n"
				+ "list:c:zone  :  lists the cards of a zone\n"
				+ "list:c:playerid:zone  :  lists the cards of a player's zone\n"
				+ "list:rounds  :  lists the rounds (doh !)\n"
				+ "list:ar  :  lists the action rules\n";
	}

}
